package com.health.mapp.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.health.db.model.IHisIMMessage;
import com.health.db.model.UserLIST;
import com.health.db.service.msg.IIHisIMMessageService;
import com.health.db.service.sm.ISMSSendService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.FeedBackReq;
import com.health.mapp.bean.FeedBackResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.PropertiesUtil;
import com.health.mapp.utils.Response;

/**
 * Created by helili on 2017/2/20.
 */

public class FeedBackImpl implements ActionInterface {

    private IIHisIMMessageService iHisIMMessageService;

    private IUserLISTService userLISTService;
    
    private  ISMSSendService sMSSendService;

    /**
     * 
     * feedDesc
     * phone
     * 
     * 
     */
    @Override
    public Response execute(Header header, Object reqBean) throws Exception {

        FeedBackReq req = (FeedBackReq) reqBean;
        FeedBackResp resp = new FeedBackResp();

        //参数校验
        String feekDesc = req.getFeekDesc();
        Long userId = req.getUserId();
    /*  if (userId == null) { 
            resp.setErrorCode(-1);
            resp.setErrorMsg("反馈用户id不能为空");
            return resp;
        }*/
        if (!StringUtils.isNotBlank(feekDesc)) {
            resp.setErrorCode(-1);
            resp.setErrorMsg("反馈描述不能为空");
            return resp;
        }
        IHisIMMessage feedback = new IHisIMMessage();
        feedback.setMessageText(feekDesc);
        
        if (null != userId &&userId!=0) { 
	        UserLIST userLIST = userLISTService.selectByUserId(userId);
	        if(userLIST !=null){
	            feedback.setSenderid(userId);
	            feedback.setSenderName(userLIST.getUserName());
	        }
        }
        feedback.setMessageTime(new Date());
        Map<String,String> itemDataMap = new HashMap<String,String>();
        //图片名称
        itemDataMap.put("Message_File",req.getFeekFile());
        itemDataMap.put("feekback_Phone",req.getFeekPhone());
        itemDataMap.put("feekback_Email",req.getFeekEmail());
        feedback.setItemData(JSON.toJSONString(itemDataMap));
        iHisIMMessageService.insert(feedback);
        
        
        String operationPhone =PropertiesUtil.getString("operationPhone");
        
        if (StringUtils.isNotEmpty(operationPhone)) {
        	String[] phones = operationPhone.split(",");
        	
        	for (int i = 0; i < phones.length; i++) {
        		sMSSendService.notice85730029(phones[i], feekDesc, req.getFeekPhone());
			}
		}
        return resp;
    }

   
 


	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}





	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}





	public IIHisIMMessageService getiHisIMMessageService() {
		return iHisIMMessageService;
	}



	public void setiHisIMMessageService(IIHisIMMessageService iHisIMMessageService) {
		this.iHisIMMessageService = iHisIMMessageService;
	}





	public ISMSSendService getsMSSendService() {
		return sMSSendService;
	}





	public void setsMSSendService(ISMSSendService sMSSendService) {
		this.sMSSendService = sMSSendService;
	}
    
	
    
    
}
